package bg.uni_sofia.conf_manager.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@Entity
@NamedQueries({
		@NamedQuery(name = "getAllEmployees", query = "SELECT e FROM Employee e"),
		@NamedQuery(name = "getEmployeeById", query = "SELECT e FROM Employee e WHERE e.id = :id"),
		@NamedQuery(name = "updateEmployeeIsActiveById", query = "UPDATE Employee e SET e.isActive = :isActive WHERE e.id = :id"),
		@NamedQuery(name = "getEmployeeByName", query = "SELECT e FROM Employee e WHERE e.username = :username") })
@XmlRootElement
public class Employee implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5603256862589881910L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	private String username;
	private String password;
	private String email;
	private String name;
	@ManyToOne
	private Company company;
	private boolean isActive;
	@Temporal(TemporalType.TIMESTAMP)
	private Date lastLogin;
	@OneToMany
	@XmlInverseReference(mappedBy = "employee")
	private List<Conference> conferences;

	public List<Conference> getConferences() {
		return conferences;
	}

	public void setConferences(List<Conference> conferences) {
		this.conferences = conferences;
	}

	public Employee() {

	}

	public Employee(String username, String password, String email,
			String name, Company company, boolean isActive, Date lastLogin) {
		super();
		this.username = username;
		this.password = password;
		this.email = email;
		this.name = name;
		this.company = company;
		this.isActive = isActive;
		this.lastLogin = lastLogin;
		conferences = new ArrayList<Conference>();
	}

	public long getId() {
		return id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@XmlTransient
	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public Date getLastLogin() {
		return lastLogin;
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}
	
	public void addConference(Conference conference){
		if(conference!=null){
			getConferences().add(conference);
		}
	}

}
